package com.youlai.system.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlai.common.model.Option;
import com.youlai.common.result.PageResult;
import com.youlai.common.result.Result;
import com.youlai.system.entity.SysOrgEntity;
import com.youlai.system.model.query.OrgPageQuery;
import com.youlai.common.plugin.dupsubmit.annotation.PreventDuplicateSubmit;
import com.youlai.system.service.SysOrgService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "组织管理接口(mybatisplus例子)")
@RestController
@RequestMapping("/api/v1/org")
@RequiredArgsConstructor
public class SysOrgController {

    private final SysOrgService service;

    @Operation(summary = "分页列表")
    @GetMapping("/page")
    public PageResult<SysOrgEntity> getPage(
            @ParameterObject OrgPageQuery queryParams
    ) {
        PageResult<SysOrgEntity> result = service.queryPage(queryParams);
        return result;
    }

    @Operation(summary = "获取表单数据")
    @GetMapping("/{id}/form")
    public Result<SysOrgEntity> getForm(
            @Parameter(description ="字典ID") @PathVariable Long id
    ) {
        SysOrgEntity formData = service.getForm(id);
        return Result.success(formData);
    }

    @Operation(summary = "新增")
    @PostMapping
    @PreAuthorize("@ss.hasPerm('sys:org:add')")
    @PreventDuplicateSubmit
    public Result saveOrUpdate(
            @RequestBody SysOrgEntity form
    ) {
        boolean result = service.saveOrUpdate(form);
        return Result.judge(result);
    }

    @Operation(summary = "修改")
    @PutMapping("/{id}")
    @PreAuthorize("@ss.hasPerm('sys:org:edit')")
    public Result update(
            @PathVariable Long id,
            @RequestBody SysOrgEntity form
    ) {
        boolean status = service.saveOrUpdate(form);
        return Result.judge(status);
    }

    @Operation(summary = "删除")
    @DeleteMapping("/{ids}")
    @PreAuthorize("@ss.hasPerm('sys:org:del')")
    public Result deleteByIds(
            @Parameter(description ="主键ID，多个以英文逗号(,)拼接") @PathVariable String ids
    ) {
        boolean result = service.deleteByIds(ids);
        return Result.judge(result);
    }

    @Operation(summary = "字典下拉列表")
    @GetMapping("/{typeCode}/options")
    public Result<List<Option>> listDictOptions(
            @Parameter(description ="字典类型编码") @PathVariable String typeCode
    ) {
        List<Option> list = service.listDictOptions(typeCode);
        return Result.success(list);
    }

}
